package com.winning.devops.batch.reader.ch01.config;

import com.winning.devops.batch.reader.ch01.DemoItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * @author chensj
 * @date 2020-02-29 20:15
 */
@Configuration
public class ItemReaderDemoConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    @Bean
    public Job itemReaderDemoJob() {
        return jobBuilderFactory.get("itemReaderDemoJob")
                .start(itemReaderDemoStep())
                .build();
    }

    @Bean
    public Step itemReaderDemoStep() {
        return stepBuilderFactory.get("itemReaderDemoStep")
                .<String, String>chunk(2)
                .reader(demoItemReader())
                .writer(list -> {
                    for (String item : list) {
                        System.out.println(item + "...");
                    }
                })
                .build();
    }

    @Bean
    public DemoItemReader demoItemReader() {
        // 传递数据
        List<String> list = Arrays.asList("java", "spring", "mybatis", "hibernate", "jpa", "apollo", "jenkins");
        return new DemoItemReader(list);
    }

}
